// https://leetcode.cn/problems/valid-parentheses/?envType=problem-list-v2&envId=o01seMu2

// 检查括号是否配对
pub fn is_valid(s: String) -> bool {
    let mut stack: Vec<char> = Vec::new();
    for c in s.chars() {
        let last = stack.last();
        match c {
            '}' => {
                if let Some('{') = last {
                    stack.pop();
                    continue;
                }
            },
            ')' => {
                if let Some('(') = last {
                    stack.pop();
                    continue;
                }
            },
            ']' => {
                if let Some('[') = last {
                    stack.pop();
                    continue;
                }
            },
            _ => {}
        }
        stack.push(c);
    }

    return stack.is_empty();
}

#[test]
fn test_is_valid() {
    assert!(is_valid(String::from("()")));
    assert!(is_valid(String::from("()[]")));
    assert!(is_valid(String::from("()[]{}")));
    assert!(is_valid(String::from("{()[]{})")));
    assert!(is_valid(String::from("({())[]{})")) == false);
}
